package com.jwd.avmanager.repository;

import com.jwd.avmanager.entity.*;
import com.jwd.avmanager.mapper.GPSQryMapper;
import com.jwd.avmanager.mapper.LoginMapper;
import com.jwd.avmanager.mapper.StaffInfoMapper;
import com.jwd.avmanager.pojo.*;
import com.jwd.avmanager.util.DateUtils.TimeJudgeUtils;
import com.jwd.avmanager.util.DebugLogger;
import com.jwd.avmanager.util.JodaTimeUtil;
import com.jwd.avmanager.util.MD5Util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class BDMybais {
    //    private SqlSession sqlSession;
//    private LoginMapper loginMapper;
//    private GPSQryMapper gpsQryMapper;
//    private StaffInfoMapper staffMapper;
    private SqlSessionFactory factory;
    private static BDMybais instance;

    public BDMybais() {
        //添加配置文件
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取SqlSessionFactoryBuilder  构造器
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 获取 SqlSessionFactory 工厂对象
        factory = sqlSessionFactoryBuilder.build(resourceAsStream);

        // 获取sqlsession
//        sqlSession = build.openSession(true);
    }

    public static BDMybais getInstance() {
        if (instance == null) {
            synchronized (DBManager.class) {
                if (instance == null) {
                    instance = new BDMybais();
                }
            }
        }

        return instance;
    }

//    private void getloginServer(){
//        if(loginMapper == null){
//            if(sqlSession == null){
//                this.getInstance();
//            }
//            loginMapper = getloginMapper();
//        }
//    }

//    public LoginMapper getloginMapper() {
//        if (sqlSession == null) {
//            this.getInstance();
//        }
//        // 获取mapper接口对象    底层代理模式
//        LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
//        return mapper;
//    }

    //    ------------------------Audio----------------------------------------------------------
    public List<Audiostaffdevic> getAudioDevic(Integer audioId) {
//        getloginServer();
        SqlSession sqlSession = factory.openSession();
        try  {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            List<Audiostaffdevic> audioDevic = mapper.getAudioDevic(audioId);
            return audioDevic;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
//
//        List<Audiostaffdevic> audioDevic = loginMapper.getAudioDevic(audioId);
//        return audioDevic;
    }

    //    ------------------------file----------------------------------------------------------
    public boolean setFile(SysDoc sysDoc) {
//        getloginServer();
//        boolean set = loginMapper.setFile(sysDoc);
//        return set;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            boolean setFile = mapper.setFile(sysDoc);
            sqlSession.commit();
            return setFile;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        } finally {
            sqlSession.close();
        }
    }

    public Integer getFileCount(String fileName) {
//        getloginServer();
//        Integer fileCount = loginMapper.getFileCount(fileName);
//        return fileCount;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
//            fileName= fileName.replace("\\","\\\\");
            return mapper.getFileCount(fileName);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    //    ------------------------FtpServer----------------------------------------------------------
    public FtpServer getFtpServer(Integer id) {
//            getloginServer();
//            FtpServer ftpServer = loginMapper.getFtpServer(id);
//            return ftpServer;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getFtpServer(id);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }
//
//函数版登录
    public void terminalLogin(TerminalLogin terminalLogin) {
//        getloginServer();
//        loginMapper.terminalLoginProc(terminalLogin);
        SqlSession sqlSession = factory.openSession();
        try{
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            mapper.terminalLoginProc(terminalLogin);
//
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

//  Sql版登录
    public TerminalLogin terminalLoginV2(TerminalLogin terminalLogin) {
        SqlSession sqlSession = factory.openSession();

        try{
            String date = JodaTimeUtil.date2Str(new Date());

            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            TerminalLogin terminalLogin1 = mapper.getloginProc(terminalLogin.getDevNo());
            System.out.println(terminalLogin1.toString());
            terminalLogin1.setIMEI(terminalLogin.getIMEI());
            terminalLogin1.setDevNo(terminalLogin.getDevNo());
            terminalLogin1.setAddDate(date);
            terminalLogin1.setSupportVideo(terminalLogin.getSupportVideo());
            terminalLogin1.setType(terminalLogin.getType());
            boolean b = mapper.UploginDevic(terminalLogin1);
            if(!b){
                System.out.println(1);
                sqlSession.rollback();
            }
            boolean b1 = mapper.UploginUserInfo(terminalLogin1);
            if(!b1){
                System.out.println(2);
                sqlSession.rollback();
            }
            boolean b2 = mapper.InDevicLogin(terminalLogin1);
            System.out.println("B2");
            if(!b2){
                System.out.println(3);
                sqlSession.rollback();
            }
            terminalLogin1.setIsLogin(1);
            sqlSession.commit();
            return terminalLogin1;
        } catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public void terminalUnLogin(TerminalUnLogin terminalUnLogin) {
//        getloginServer();
//        loginMapper.terminalUnLogin(terminalUnLogin);
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            mapper.terminalUnLogin(terminalUnLogin);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

//    退出登录
    public boolean terminalUnLogin2(TerminalUnLogin terminalUnLogin) {
        SqlSession sqlSession = factory.openSession();
        try {

            String date = JodaTimeUtil.date2Str(new Date());
            terminalUnLogin.setAddDate(date);
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);

            LogQuitVrify logQuitVrify = mapper.NologVrify(terminalUnLogin.getDev_idx());
            if(!logQuitVrify.isuIsLogin()  && !logQuitVrify.isDvLastStatus()){
                System.out.println("校验true");
                return true;
            }

            boolean b = mapper.UpNologDevic(terminalUnLogin);
            if(!b){
                System.out.println(1);
                sqlSession.commit();
                return false;
            }
            boolean b1 = mapper.UpNologUserInfo(terminalUnLogin);
            if(!b1){
                System.out.println(2);
                sqlSession.commit();
                return false;
            }
            boolean b2 = mapper.InDevicNoLog(terminalUnLogin);
            if(!b2){
                System.out.println(3);
                sqlSession.commit();
                return false;
            }
            sqlSession.commit();
            return true;
        } catch (Exception e) {
            sqlSession.commit();
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public Login login(String device, String pwd) {
//        String md5Str = MD5Util.getMD5Str(pwd);
//        loginMapper = getloginMapper();
//        Login getlogin = loginMapper.getlogin(device, md5Str);
//        if(getlogin == null) {
//            return null;
//        }
//        getlogin.setFlag(true);
//        if(getlogin.getuIsLogin() == 0){
//            DebugLogger.debug(getlogin.toString());
//            alterData(getlogin);
//            getlogin = loginMapper.getlogin(device, md5Str);
//            getlogin.setFlag(true);
//            commit();
//            DebugLogger.debug(getlogin.toString());
//        } else {
//            getlogin.setFlag(false);
//        }
//
//        return getlogin;
        String md5Str = MD5Util.getMD5Str(pwd);
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper loginMapper = sqlSession.getMapper(LoginMapper.class);
            Login getlogin = loginMapper.getlogin(device, md5Str);
            if (getlogin == null) {
                return null;
            }
            getlogin.setFlag(true);
            if (getlogin.getuIsLogin() == 0) {
                DebugLogger.info(getlogin.toString());
                alterData(getlogin);
                getlogin = loginMapper.getlogin(device, md5Str);
                getlogin.setFlag(true);
                sqlSession.commit();
                DebugLogger.info(getlogin.toString());
            } else {
                getlogin.setFlag(false);
            }
            return getlogin;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public Users getUsers(int uDvId) {
//        Users users = loginMapper.getUsers(uDvId);
//        return users;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getUsers(uDvId);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public Users getUser(int uId) {
//        Users users = loginMapper.getUser(uId);
//        return users;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getUser(uId);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }


    public TokenData gettoken(String token) {
//        Users users = loginMapper.getUser(uId);
//        return users;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getToken(token);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public void alterData(Login getlogin) {

//        DebugLogger.debug(loginMapper);//
//        loginMapper.alterState(1, getlogin.getDevicID(), getlogin.getIMEI());
//        DebugLogger.debug("完成");
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            mapper.alterState(1, getlogin.getDevicID(), getlogin.getIMEI());
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public List<Group> getGroup(int uDvId) {

//        List<Group> getgroup = loginMapper.getgroup(uDvId);
//        return getgroup;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getgroup(uDvId);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public List<Group> getgroupLog(int uDvId) {

        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getgroupLog(uDvId);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public List<GUser> getuserList(int uDvId) {

//        List<GUser> userList = loginMapper.getuserList(uDvId);
//        return userList;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getuserList(uDvId);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public List<GUser> getGUList(int uDvId) {
//        List<GUser> userList = loginMapper.getGUList(uDvId);
//        return userList;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getGUList(uDvId);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public void getGpsSeva(Gps gpsSeva) {
//        loginMapper.insGPS(gpsSeva);
//        commit();
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            mapper.insGPS(gpsSeva);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

//sqlGPS
    public boolean getGpsSevaV2(Gps gpsSeva ,Integer devId) {

        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            Integer userID = mapper.getUserID(devId);
            String currentTime = JodaTimeUtil.getCurrentDateTime();
            GpsMes gpsMes = new GpsMes();
            gpsMes.setDevId(devId);
            gpsMes.setUserId(userID);
            gpsMes.setAddDate(currentTime);

            Integer devicUseID = mapper.getDevicUseID(gpsMes);

            DebugLogger.info("devicUseID:"+devicUseID );
            if(devicUseID == null || devicUseID <= 0 ){

                currentTime = JodaTimeUtil.getCurrentDateTime();
                gpsMes.setAddDate(currentTime);
                boolean b = mapper.setDevicUse(gpsMes);
                if(b){
                    devicUseID = gpsMes.getID();
                    System.out.println(gpsMes.toString());
                } else {
                    return false;
                }

            }
            gpsSeva.setAddDate(currentTime);
            gpsSeva.setDevicUseID(devicUseID);

            DebugLogger.info("GPS_setData_id:"+userID + "---GpsMes.class:"+ gpsSeva+"");

            GpsV2  gpsV2 = new GpsV2();
            gpsV2.setgLongitude(gpsSeva.getLon84());
            gpsV2.setgLatitude(gpsSeva.getLat84());
            gpsV2.setgLonBD(gpsSeva.getLonBD());
            gpsV2.setgLatBD(gpsSeva.getLatBD());
            gpsV2.setgLonGCJ(gpsSeva.getLonGCJ());
            gpsV2.setgLatGCJ(gpsSeva.getLatGCJ());
            gpsV2.setgAltitude(gpsSeva.getAltitude());
            gpsV2.setgSpeed(gpsSeva.getSpeed());
            gpsV2.setgDirec(gpsSeva.getDirec());
            gpsV2.setgGPSDate(gpsSeva.getGPSDate());
            gpsV2.setgReceiveDate(gpsSeva.getAddDate());
            gpsV2.setgTempera(gpsSeva.getTempera());
            gpsV2.setgSatelliteNumber(gpsSeva.getSatelliteNumber());
            gpsV2.setgGPSValid(gpsSeva.getGPSValid());
            gpsV2.setDevicID(gpsSeva.getDev_idx());
            gpsV2.setDevicUseID(gpsSeva.getDevicUseID());
            System.out.println(gpsV2.toString()  +"  ->>>");
            boolean b = mapper.setSysGPS(gpsV2);
            if(!b){
                return false;
            }

            sqlSession.commit();
            return true;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    //    -------------------------WedRTC  聊天房间---------------用户信息区----------------------------------------------------
    public WebRtc getWedRTCSeva(WebRtc wedRtc) {
//        getloginServer();
//        loginMapper.getWedRTC(wedRtc);
//        return wedRtc;
        System.out.println(wedRtc.toString());
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            mapper.getWedRTC(wedRtc);
            return wedRtc;

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }


    public WebRtc getWedRTCSevaV2(WebRtc wedRtc) {
//        getloginServer();
//        loginMapper.getWedRTC(wedRtc);
//        return wedRtc;
//        System.out.println(wedRtc.toString());
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            WebRtcUserInfo wedRTCUserInfo = mapper.getWedRTCUserInfo(wedRtc.getUserID());
            if(wedRTCUserInfo == null){

            }
            List<WebRtcAudioTalkStatus> wedRTCATS = mapper.getWedRTCATS(wedRtc.getUserID());

            if(wedRTCATS.size()<=0){
                wedRtc.setOnTalk(0);
                if(wedRTCUserInfo.getuIsSysMana() == 1){
                    wedRtc.setTalkGrade(5);
                } else if(wedRTCUserInfo.getuIsDepartMana() == 1) {
                    wedRtc.setTalkGrade(30);
                } else {
                    wedRtc.setTalkGrade(40);
                }

            } else {
                wedRtc.setOnTalk(1);

                Integer wedRTCSDepartment = mapper.getWedRTCSDepartment(wedRTCATS.get(0).getAudioGroupID());
//                Integer wedRTCAudioGroup = mapper.getWedRTCAudioGroup(wedRTCATS.get(0).getAudioGroupID());
                if(wedRTCSDepartment != null){  //群组
                    wedRtc.setTalkGrade(50);
                }  else  {
                    //临时组
                    if(wedRTCUserInfo.getuIsSysMana() == 1){
                        wedRtc.setTalkGrade(5);
                    } else if(wedRTCUserInfo.getuIsDepartMana() == 1) {
                        wedRtc.setTalkGrade(30);
                    } else {
                        wedRtc.setTalkGrade(40);
                    }

                    Integer wedRTCATSUserId = mapper.getWedRTCAudioTalkStatus(wedRTCATS.get(0).getAudioGroupID(), wedRTCATS.get(0).getUserID());
                    if(wedRTCATSUserId == null){
                        wedRTCATSUserId = 0;
                    }
                    WebRtcUserInfo wedRTCUserInfo_2 = mapper.getWedRTCUserInfo(wedRTCATSUserId);

                    Integer TargetTalkGrade = null;
                    if(wedRTCATSUserId != null && wedRTCATSUserId > 0) {
                        if (wedRTCUserInfo_2.getuIsSysMana() == 1) {
                            TargetTalkGrade=(5);
                        } else if (wedRTCUserInfo_2.getuIsDepartMana() == 1) {
                            TargetTalkGrade=(30);
                        } else {
                            TargetTalkGrade=(40);
                        }
                    }
                    if(TargetTalkGrade != null){
                        if(TargetTalkGrade< wedRtc.getTalkGrade()){
                            wedRtc.setTalkGrade(TargetTalkGrade);
                        }
                    }
                }


            }
            System.out.println(1);
            WedRTCAudioGroup wedRTCAudioGroup = new WedRTCAudioGroup();
            wedRTCAudioGroup.setAudioGroupName(TimeJudgeUtils.getNowDate());
            wedRTCAudioGroup.setGroupType(1);
            //生成房间号
            mapper.setWedRTCAudioGroup(wedRTCAudioGroup);

            wedRtc.setRoomID(wedRTCAudioGroup.getAudioGroupID());
            wedRtc.setDevicID(wedRTCUserInfo.getuDvID());
            wedRtc.setLoginType(wedRTCUserInfo.getuLoginType());
            wedRtc.setUserName(wedRTCUserInfo.getuName());
            wedRtc.setOnLine(wedRTCUserInfo.getuIsLogin());
            sqlSession.commit(); // 提交事务
            return wedRtc;

        } catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public SOSSave getSosSave(SOSSave sosSave) {
//        getloginServer();
//        loginMapper.getSosSave(sosSave);
//        return sosSave;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            mapper.getSosSave(sosSave);
            return sosSave;

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public SOSSaveV2 getSosSaveV2(SOSSaveV2 sosSave) {

        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            mapper.getSosSaveV2(sosSave);
            return sosSave;

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public SOSSaveV2 getSosSaveSQL(Integer devIdx) {
        SqlSession sqlSession = factory.openSession();
        try {
            // 时间
            String currentTime = JodaTimeUtil.date2Str(new Date());
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            SOSSaveV2 sosSQL = mapper.getSosSQL(devIdx);
            SaveSosSQL saveSosSQL = new SaveSosSQL();
            saveSosSQL.setDevIdx(devIdx);
            saveSosSQL.setUserName(sosSQL.getUserName());
            saveSosSQL.setUserId(sosSQL.getDevIdx());
            saveSosSQL.setAddDate(currentTime);

            boolean fla = mapper.saveSosSQL(saveSosSQL);
            if(!fla){
                return null;
            }
            System.out.println(saveSosSQL.toString());
            sosSQL.setDevicSOSID(saveSosSQL.getDevicSOSID());
            sqlSession.commit(); // 提交事务
            return sosSQL;

        } catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public SOSSaveV2 getSosSQL(Integer devIdx) {

        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            SOSSaveV2 sosSQL = mapper.getSosSQL(devIdx);
            return sosSQL;

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

//    public Integer saveSosSQL(SaveSosSQL saveSosSQL) {
//
//        SqlSession sqlSession = factory.openSession();
//        try {
//            //操作数据库
//            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
//            Integer Id = mapper.saveSosSQL(saveSosSQL);
//            return Id;
//
//        } catch (Exception e) {
//            throw new RuntimeException("mybatis exception:" + e.getMessage());
//        }finally {
//            sqlSession.close();
//        }
//    }




    public boolean alterSosSaveUrl(AlterSOSSave alterSOS) {
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            boolean sosSaveUrl = mapper.alterSosSaveUrl(alterSOS);
            return sosSaveUrl;

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public RTCAddRoom getRTCAddRoom(RTCAddRoom rtcAddRoom) {
//        getloginServer();
//        loginMapper.getRTCAddRoom(rtcAddRoom);
//        return rtcAddRoom;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            mapper.getRTCAddRoom(rtcAddRoom);
            return rtcAddRoom;

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }
//    加入房间记录
    public RTCAddRoom getRTCAddRoomV2(RTCAddRoom rtcAddRoom) {

        SqlSession sqlSession = factory.openSession();
        try {

            String currentTime = JodaTimeUtil.date2Str(new Date());
            rtcAddRoom.setAddDate(currentTime);
            System.out.println(rtcAddRoom.toString());
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            Integer rtcAddRoomID = mapper.getRTCAddRoomID(rtcAddRoom);
            System.out.println(rtcAddRoomID);
            if(rtcAddRoomID == null || rtcAddRoomID <=0 ){
                System.out.println("rtcAddRoom->"+rtcAddRoom);
                boolean rtcAddRoomRecord = mapper.getRTCAddRoomRecord(rtcAddRoom);
                boolean rtcAddRoomRecord2 = mapper.getRTCAddRoomRecord2(rtcAddRoom);
                if(!rtcAddRoomRecord) {
                    return null;
                }
            }
            sqlSession.commit();
            return rtcAddRoom;

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    //    退出WebRTC房间
    public RTCAddRoom getRTCDelRoomV2(RTCAddRoom rtcAddRoom) {

        SqlSession sqlSession = factory.openSession();
        try {
            String currentTime = JodaTimeUtil.date2Str(new Date());
            rtcAddRoom.setAddDate(currentTime);

            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            Integer rtcAddRoomID = mapper.getRTCAddRoomID(rtcAddRoom);
            System.out.println(rtcAddRoomID);
            if(rtcAddRoomID != null && rtcAddRoomID > 0 ){
                boolean delRooId = mapper.delRooId(rtcAddRoom);
                if(!delRooId){
                    return null;
                }
                boolean rtcDelRoomRecord = mapper.getRTCDelRoomRecord(rtcAddRoom);
                if(!rtcDelRoomRecord){
                    return null;
                }
            }
            sqlSession.commit();
            return rtcAddRoom;

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public VideoServer getVideoServerQry(int id) {
//        getloginServer();
//        VideoServer videoServerQry = loginMapper.getVideoServerQry(id);
//        return videoServerQry;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            return mapper.getVideoServerQry(id);

        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }
//    -----------------------------------------------用户信息区-----------------------------------------------------

    //    ------------------------------------GPSQry------------------------------------------------------------------
//    private void getGPSServer() {
//        if (gpsQryMapper == null) {
//            if (sqlSession == null) {
//                this.getInstance();
//            }
//            gpsQryMapper = getGPSQryMapper();
//        }
//    }

//    public GPSQryMapper getGPSQryMapper() {
//        if (sqlSession == null) {
//            this.getInstance();
//        }
//        // 获取mapper接口对象    底层代理模式
//        GPSQryMapper mapper = sqlSession.getMapper(GPSQryMapper.class);
//        return mapper;
//    }

    public GpsQry getGpsQry(int id) {
//        getGPSServer();
//        GpsQry gps = gpsQryMapper.getGPS(id);
//        return gps;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            GPSQryMapper mapper = sqlSession.getMapper(GPSQryMapper.class);
            return mapper.getGPS(id);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    //    -------------------------------StaffInfoMapper--------------------------------------------------------------
//    private void getStaffServer() {
//        if (sqlSession == null) {
//            this.getInstance();
//        }
//        staffMapper = getStaffMapper();
//    }

//    public StaffInfoMapper getStaffMapper() {
//        if (sqlSession == null) {
//            this.getInstance();
//        }
//        // 获取mapper接口对象    底层代理模式
//        StaffInfoMapper mapper = sqlSession.getMapper(StaffInfoMapper.class);
//        return mapper;
//    }

    public List<StaffInfo> getStaffData(Integer id) {
//        getStaffServer();
//        DebugLogger.debug(id);
//        List<StaffInfo> staffInfos = staffMapper.getStaffInfo(id);
//        return staffInfos;
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            StaffInfoMapper mapper = sqlSession.getMapper(StaffInfoMapper.class);
            return mapper.getStaffInfo(id);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }
    //TODO  修改
    public List<SysUserinfo> getParentGroup(Integer id) {

        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            StaffInfoMapper mapper = sqlSession.getMapper(StaffInfoMapper.class);
            Integer dvDepartmentID = mapper.getdevic(id);
            if(dvDepartmentID == null){
                return null;
            }
            return mapper.getParentGroup(dvDepartmentID);
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public List<GUsetDep> getUserDep(int devIdx) {
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            List<GUsetDep> userDeps = mapper.getUserDep(devIdx);
            if(userDeps == null){
                return null;
            }
            return userDeps;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }
    public boolean BrushDeviceUerStatus() {
        SqlSession sqlSession = factory.openSession(true);
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
            boolean refreshLoginDev = mapper.refreshLoginDev();
            boolean refreshLoginUser = mapper.refreshLoginUser();
            return true;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

    public boolean isOnlineStatus(String device) {
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);

            boolean refreshLoginDev = mapper.isOnlineStatus(device);

            return refreshLoginDev;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }

    }

    public Users getuLoginName(String uLoginName) {
        SqlSession sqlSession = factory.openSession();
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);

            Users users = mapper.getuLogin(uLoginName);

            return users;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }

    }

    public boolean setuLoginName(AlterUsetInfo alterUsetInfo) {
        SqlSession sqlSession = factory.openSession(true);
        try {
            //操作数据库
            LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);

            boolean setuLoginName = mapper.setuLoginName(alterUsetInfo);

            return setuLoginName;
        } catch (Exception e) {
            throw new RuntimeException("mybatis exception:" + e.getMessage());
        }finally {
            sqlSession.close();
        }
    }

//    -----------------------------事务区-----------------------------------------------------------------------------

    //手动提交事务
//    public void commit() {
//        if (sqlSession == null) {
//            this.getInstance();
//        }
//        sqlSession.commit();
//    }
}
